/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-08-06 08:07:52
 * @LastEditTime: 2021-09-18 15:23:02
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */
#include "kernel.h"
#include "ft_io.h"
#include "parameters.h"
#include "early_uart.h"

#define EARLY_UART_BASE         FT_UART1_BASE_ADDR
#define EARLY_UART_UARTDR       (EARLY_UART_BASE + 0x0)  /* UART 数据寄存器地址 */
#define EARLY_UART_UARTFR       (EARLY_UART_BASE + 0x18) /* UART 状态寄存器地址 */
#define EARLY_UART_TXFF         BIT(5)  /* 发送 FIFO 已满标志位 */
#define EARLY_UART_RXFE         BIT(4)  /* 接收 FIFO 为空标志位 */
#define EARLY_UART_DATA_MASK    GENMASK(7, 0)

void OutByte(s8 byte)
{
    /* wait until tx fifo is not full */
    while ((FtIn32(EARLY_UART_UARTFR) & EARLY_UART_TXFF) == EARLY_UART_TXFF)
    {

    }

    FtOut32(EARLY_UART_UARTDR, (((u32)byte) & EARLY_UART_DATA_MASK));
}

char GetByte(void)
{
    /* wait until rx fifo is not empty */
    while ((FtIn32(EARLY_UART_UARTFR) & EARLY_UART_RXFE) == EARLY_UART_RXFE)
    {
        
    }

    return (char)(EARLY_UART_DATA_MASK & FtIn32(EARLY_UART_UARTDR));
}
